package com.xdw.uploaddemo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class UploadFileConfig implements WebMvcConfigurer {
    @Value("${fileUploadPathWindow}")
    private String fileUploadPathWindow;
    @Value("${fileUploadPathLinux}")
    private String fileUploadPathLinux;
    @Value("${uploadFileUrlPrefix}")
    private String uploadFileUrlPrefix;


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String os = System.getProperty("os.name");
        if(os.toLowerCase().contains("window")){
            //若当前系统是window系统
            registry.addResourceHandler(uploadFileUrlPrefix+"**").addResourceLocations("file:"+fileUploadPathWindow);
        }else {
            registry.addResourceHandler(uploadFileUrlPrefix+"**").addResourceLocations("file:"+fileUploadPathLinux);
        }
        /*
         * 说明：增加虚拟路径(经过本人测试：在此处配置的虚拟路径，用springboot内置的tomcat时有效，
         * 用外部的tomcat也有效;所以用到外部的tomcat时不需在tomcat/config下的相应文件配置虚拟路径了,阿里云linux也没问题)
         */

//        registry.addResourceHandler("/upload/video/**").addResourceLocations("file:D:/upload/video/");

        //阿里云(映射路径去除盘符)
        //registry.addResourceHandler("/ueditor/image/**").addResourceLocations("/upload/image/");
        //registry.addResourceHandler("/ueditor/video/**").addResourceLocations("/upload/video/");
    }
}